package com.hc.handler;

import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.ex.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     */
    @ResponseBody
    @ExceptionHandler(GlobalException.class)
    public Result handleException(GlobalException e) {
        // 打印异常信息
        log.error("### 异常信息:{} ###", e.getMessage());
        return ResultUtil.fail().setCode(e.getCode()).setMsg(e.getMsg());
    }
}
